package 排序;

//插入排序
public class chaRutext1 {
    public static void main(String[] args) {
        int[] arr = {3, 1, 2, 5, 4, 6};
        charu(arr);
    }

    private static void charu(int[] arr) {
        //第一步：找到无序的开始索引
        int startIndex = -1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > arr[i + 1]) {
                startIndex = i + 1;
                break;
            }
        }

        //第二步： 开始排序
        for (int i = startIndex; i < arr.length; i++) {
            int j = i;
            while (j > 0 && arr[j] < arr[j - 1]) {
                int t = arr[j];
                arr[j] = arr[j - 1];
                arr[j - 1] = t;
                j--;
            }
        }

        //打印
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

}
